<?php
class ProvinceService {
	
	private $_type = array (1 => "省份", 2 => "地级市", 3 => "区或县级市" );
	
	public function getType($type, $id) {
		switch ($type) {
			case 1 :
				$rs = ProvinceModel::instance ()->getAllProvince ();
				break;
			case 2 :
				$rs = ProvinceModel::instance ()->getCityByProvinceId ( $id );
				break;
			case 3 :
				$rs = ProvinceModel::instance ()->getAreasByCityId ( $id );
				break;
		
		}
		return json_encode ( $rs );
	}
	
	public function getAllProvince() {
		return ProvinceModel::instance ()->getAllProvince ();
	}
	
	public function getAreasByCityId($cityId) {
		return ProvinceModel::instance ()->getAreasByCityId ( $cityId );
	}
	
	public function getCityByProvinceId($provinceid) {
		return ProvinceModel::instance ()->getCityByProvinceId ( $provinceid );
	}
	
	public function getProvinceById($provinceid) {
		return ProvinceModel::instance ()->getProvinceById ( $provinceid );
	}
	public function getAreasById($areaid) {
		return ProvinceModel::instance ()->getAreasById ( $areaid );
	}
	public function getCityById($cityid) {
		return ProvinceModel::instance ()->getCityById ( $cityid );
	}
}

class ProvinceModel extends Db {
	
	private $_province = 'e_region'; //省份
	

	public function getAllProvince() {
		return $this->getAll ( $this->_province, array ('fid' => 1 ),null,'szm ASC' );
	}
	
	public function getProvinceById($provinceid) {
		return $this->getOne ( $this->_province, array ('id' => $provinceid ) );
	}
	
	public function getAreasByCityId($cityid) {
		return $this->getAll ( $this->_province, array ('fid' => $cityid ) ,null,'szm ASC' );
	}
	public function getAreasById($areaid) {
		return $this->getOne ( $this->_province, array ('id' => $areaid ) );
	}
	
	public function getCityByProvinceId($provinceid) {
		return $this->getAll ( $this->_province, array ('fid' => $provinceid ) ,null,'szm ASC' );
	}
	
	public function getCityById($cityid) {
		return $this->getOne ( $this->_province, array ('id' => $cityid ) );
	}
	
	/**
	 * 
	 *@return ProvinceModel
	 */
	public static function instance() {
		return parent::_instance ( __CLASS__ );
	}

}